package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.common.vo.MemberVo;
import com.opgea.crm.common.vo.ProjectDetailsVo;
import com.opgea.crm.common.vo.ProjectDocumentVo;
import com.opgea.crm.common.vo.ProjectReportVo;
import com.opgea.crm.domain.entities.Employee;
import com.opgea.crm.domain.entities.Employeeemails;
import com.opgea.crm.domain.entities.Employeephones;
import com.opgea.crm.domain.entities.ProjectDetails;
import com.opgea.crm.domain.entities.ProjectDocuments;
import com.opgea.crm.domain.modal.QualifierModel;
import com.opgea.crm.repository.ProjectDetailsRepo;

@Service
public class ProjectDetailsServiceImpl implements ProjectDetailsService {
	
	@Autowired
	ProjectDetailsRepo projectDetailsRepo; 

	@Transactional(propagation = Propagation.REQUIRED)
	public void create(ProjectDetails projectDetails) {
		projectDetailsRepo.create(projectDetails);
		
	}

	
	public ProjectDetails find(Long id) {
		return projectDetailsRepo.find(id);  
	}

	
	public List<ProjectDetails> findAll() {
		return projectDetailsRepo.findAll();
	}
	
	
	public List<ProjectDetails> findAllByCompanyId(Long id) {
		return projectDetailsRepo.findAllByCompanyId(id); 
	}
	
	
	public List<QualifierModel> findAllProjectNames(Long companyId) {
		List<ProjectDetails> projectList = projectDetailsRepo.findAllByCompanyId(companyId);
		List<QualifierModel> projectQualifiers = new ArrayList<QualifierModel>();
		for(ProjectDetails project : projectList){
			projectQualifiers.add(new QualifierModel(project.getId(), project.getProjectName()));
		}
		return projectQualifiers;
	}	

	@Override
	public List<QualifierModel> findAllMemberNamesOfProject(Long projectId) {
		List<Employee> members = projectDetailsRepo.find(projectId).getMembers(); 
		List<QualifierModel> memberList = new ArrayList<QualifierModel>();
		for(Employee employee: members){
			memberList.add(new QualifierModel(employee.getId(), employee.getFirstName()));
		}
		return memberList;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void remove(Long id) {
		
		
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void update(ProjectDetails projectDetails)
	{
		projectDetailsRepo.update(projectDetails);
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	public List<MemberVo> findMemberList(String criteria, long projectId)
	{
		List<MemberVo> list=new ArrayList<MemberVo>();
		List<Employee> emp = projectDetailsRepo.findMemberList(criteria,projectId);
		for(Employee employee: emp)
		{
			MemberVo memberVo = new MemberVo();
			memberVo.setEmployeeId(employee.getId());
			memberVo.setEmpCode(employee.getEmpCode());
			memberVo.setFirstName(employee.getFirstName());
			memberVo.setLastName(employee.getLastName());
			memberVo.setMiddleInitial(employee.getMiddleInitial());
			memberVo.setTitle(employee.getTitle());
			
			List<Employeeemails> emails =(List<Employeeemails>)employee.getEmployeeemailsCollection();
			if(emails.size()>0 && emails != null && emails.get(0) != null){
			memberVo.setEmail(emails.get(0).getAddress());
			}
			List<Employeephones> phones = new ArrayList<Employeephones>(employee.getEmployeephonesCollection());
			if(phones.size() > 0){
				if(phones != null && phones.get(0) != null ){
				memberVo.setPhone1(phones.get(0).getPhoneNo());
				}
			}
			if(phones.size() > 1){
				if(phones != null && phones.get(1) != null){
				memberVo.setPhone2(phones.get(1).getPhoneNo());
				}
			}
		
			list.add(memberVo);
	  }
     return  list;
		
	}
	
	

	@Transactional(propagation=Propagation.SUPPORTS)
	public List<MemberVo> findMemberByProjectId(long projectId) 
	{
	    List<MemberVo> list = new ArrayList<MemberVo>();
	   ProjectDetails emp = projectDetailsRepo.find(projectId);
	    for(Employee employee: emp.getMembers())
	    {
	    	MemberVo memberVo = new MemberVo();
	    	memberVo.setEmployeeId(employee.getId());
			memberVo.setEmpCode(employee.getEmpCode());
			memberVo.setFirstName(employee.getFirstName());
			memberVo.setLastName(employee.getLastName());
			memberVo.setMiddleInitial(employee.getMiddleInitial());
			memberVo.setTitle(employee.getTitle());
			
			List<Employeeemails> emails = new ArrayList<Employeeemails>(employee.getEmployeeemailsCollection());
			if(emails.size()>0 && emails!= null && emails.get(0)!= null){
			memberVo.setEmail(emails.get(0).getAddress());
			}
			List<Employeephones> phones = new ArrayList<Employeephones>(employee.getEmployeephonesCollection());
			if(phones.size() > 0){
				if(phones!=null && phones.get(0)!= null){
				memberVo.setPhone1(phones.get(0).getPhoneNo());
				}
			}
			if(phones.size() > 1){
				if(phones!=null && phones.get(1)!= null){
				memberVo.setPhone2(phones.get(1).getPhoneNo());
				}
			}
		
			list.add(memberVo);
	    }
		return list;
	}
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<QualifierModel> findAllCustomerNameByCompanyId(long companyId)
	{
		List<Employee> employees = projectDetailsRepo.findByCompanyId(companyId);
		List<QualifierModel> customerNameList = new ArrayList<QualifierModel>();
		System.out.println("Inside service impl");
		for(Employee employee : employees)
		{
			customerNameList.add(new QualifierModel(employee.getId(), employee.getFirstName()));
			System.out.println("Assign To : id : "+employee.getId()+", Value :  "+employee.getFirstName());
		}
		return customerNameList;
    }


  @Transactional(propagation=Propagation.SUPPORTS)
	public List<ProjectDetailsVo> findProjectList(String criteria, String value)
	{
		List<ProjectDetailsVo> list=new ArrayList<ProjectDetailsVo>();
		List<ProjectDetails> projects = projectDetailsRepo.findProjectList(criteria,value);
		for(ProjectDetails project:projects)
		  {
			ProjectDetailsVo projectDetailsVo = new ProjectDetailsVo();
			projectDetailsVo.setId(project.getId());
			projectDetailsVo.setProjectName(project.getProjectName());
			if(project.getCustomer()!= null)
			projectDetailsVo.setContactPerson(project.getCustomer().getFirstName());
			if(project.getPriority() != null)
			projectDetailsVo.setPriority(project.getPriority().getFieldValue());
			if(project.getStatus() != null)
			projectDetailsVo.setStatus(project.getStatus().getFieldValue());
			
			list.add(projectDetailsVo);
			  
		  }
		  return list;
	}
	
	public List<QualifierModel> getProjectSearchCriteriaTypes(){
		List<QualifierModel> projectSearchTypes = new ArrayList<QualifierModel>();
		for(ApplicationConstant.ProjectSearchCriteriaQualifier qualifier : ApplicationConstant.ProjectSearchCriteriaQualifier.values()){
			projectSearchTypes.add(new QualifierModel(Long.valueOf(qualifier.ordinal()), qualifier.name()));
		}
		return projectSearchTypes;
	}


	@Transactional(propagation=Propagation.SUPPORTS)
	public List<ProjectReportVo> findMemberReportList(Long memberProjectId)
	{
		 List<ProjectReportVo> list = new ArrayList<ProjectReportVo>();
		 ProjectDetails emp = projectDetailsRepo.find(memberProjectId);
		    for(Employee employee: emp.getMembers())
		    {
		    	ProjectReportVo projectReportVo = new ProjectReportVo();
		    	projectReportVo.setFirstName(employee.getFirstName());
		    	if(employee.getCompany()!=null)
		    	{
		    		projectReportVo.setCompanyName(employee.getCompany().getName());
		    	}
		    	List<Employeephones> phones = new ArrayList<Employeephones>(employee.getEmployeephonesCollection());
	     		if(phones.size() > 0){
	     			projectReportVo.setPhone1(phones.get(0).getPhoneNo());
	     		}
	     		
	     		List<Employeeemails> emails = new ArrayList<Employeeemails>(employee.getEmployeeemailsCollection());
	     		if(emails.size()>0 && emails != null && emails.get(0)!= null)
	     		{
		    	     projectReportVo.setEmail(emails.get(0).getAddress());
	     		}
	     		
				list.add(projectReportVo);
		    }
		    return list;
	}
	
	
  //for uploading document in project module
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void createProjectDoc(ProjectDocuments documents) {
		projectDetailsRepo.createProjectDoc(documents);
		
	}
	
	
	public List<ProjectDocumentVo> findAllByProject(long projectId){

		List<ProjectDocuments> projectDocs = projectDetailsRepo.findAllByProject(projectId);
		List<ProjectDocumentVo> docList = new ArrayList<ProjectDocumentVo>();
		
		for(ProjectDocuments doc : projectDocs){
			ProjectDocumentVo docVo = new ProjectDocumentVo();
			docVo.setDocumentId(doc.getId());
			docVo.setDocumentName(doc.getDocumentName());
			docVo.setFileName(doc.getFileName());
			docVo.setFileSize(doc.getFileSize());
			docVo.setReceiveDate(DateUtil.dateToString(doc.getReceiveDate()));
			//docVo.setUploadDate(DateUtil.dateToString(doc.getUploadedAt()));
			docVo.setDocumentType(ApplicationConstant.DocumentQualifier.values()[doc.getDocumentType()].toString());
			String content = new String(doc.getContent());
			docVo.setFileContent(content);
			//docVo.setUploadedBy(doc.getUploadedBy().getFirstName());
			docList.add(docVo);
			System.out.println("Document Vo : "+docVo);
		}
		return docList;
	}

	
	
}
